import {
	BaseUrl,
	formatDishesScore,
	scoreToFloat1
} from "./config"

/**
 * 创建评论
 */
export function reqCreateComment({
	userId,
	dishId,
	score,
	comment,
}) {
	return uni.request({
		method: "POST",
		url: BaseUrl + '/comments/create',
		data: {
			userId,
			dishId,
			score,
			comment,
		}
	}).then(res => {
		return res.data
	})
}

/**
 * 我的评论
 */
export function reqMyComments({
	dishId,
	userId
}) {
	return uni.request({
		method: "POST",
		url: BaseUrl + '/comments/mine',
		data: {
			dishId,
			userId,
			pageSize: 100,
			pageNumber: 1
		}
	}).then(res => {
		// return res.data
		res.data.rows = res.data.rows.map(e => {
			e.score = scoreToFloat1(e.score)
			return e
		})
		return res.data.rows
	})
}


/**
 * 根据菜品 id 分页获取评论
 * 菜品增加liked字段：已点赞的评论
 */
export function getCommentsByDishIdUserId({
	dishId,
	userId
}) {
	console.log("dishId", dishId);
	console.log("userId", userId);
	return uni.request({
		method: "POST",
		url: BaseUrl + '/comments/getByDishId',
		data: {
			dishId,
			userId,
			pageSize: 100,
			pageNumber: 1
		}
	}).then(res => {
		console.log(222222, res.data.rows);
		res.data.rows = res.data.rows.map(e => {
			e.score = scoreToFloat1(e.score)
			return e
		})
		return res.data.rows

		// const comments = res.data.data.comments.map(e => {
		// 	e.score = scoreToFloat1(e.score)
		// 	return e
		// })

		// // console.log("1111", comments);
		// return comments
	})
}

/**
 * 允许评论
 */
export function allowCommentByDishIdUserId({
	userId,
	dishId
}) {
	return uni.request({
		method: "GET",
		url: BaseUrl + '/comments/mayComment',
		data: {
			dishId,
			userId,
		}
	}).then(res => {
		// console.log("允许评论", res.data);
		return res.data
	})
}

/**
 * 点赞或者取消点赞
 */
export function reqLikeComment({
	userId,
	commentId
}) {

	return uni.request({
		method: "POST",
		url: BaseUrl + '/comments/liked',
		data: {
			userId,
			commentId,
		}
	}).then(res => {
		// console.log("点赞或者取消点赞", res.data);
		return res.data
	})
}

/**
 * 删除评论
 */
export function reqDeleteCommentById(commentId) {

	return uni.request({
		method: "GET",
		url: BaseUrl + '/comments/deleteById?id=' + commentId,
		// url: BaseUrl + '/comments/deleteById',
		// data: {
		// 	id: commentId
		// },

	}).then(res => {
		// console.log("删除评论", res.data);
		return res.data
	})
}


/**
 * 修改评论
 */
export function reqUpdateComment({
	commentId,
	score,
	comment,
}) {

	return uni.request({
		method: "POST",
		url: BaseUrl + '/comments/update',
		data: {
			commentId,
			score,
			comment,
		}
	}).then(res => {
		// console.log("修改评论", res.data);
		return res.data
	})
}